require(['highcharts'], function () {
    window.showDobChart = function (arr) {
        var data = arr[0];

        console.log(data);

        var initDobInfo = function () {
            if (data.info.type == "0") {//行为疑点
                $("#stnNameLi").show();
                $("#drvSctBhvLi").show();
                $("#occuTime1Li").show();
                $("#occuTime2Li").hide();

                //赋值
                $("#licPltNo").html(data.info.licPltNo);
                $("#drvName").html(data.info.drvName);
                $("#lineName").html(data.info.lineName);
                $("#stnName").html(data.info.stnName);
                $("#drvSctBhvName").html(data.info.drvSctBhvName);
                $("#occuTime1").html(data.info.occuTime);
            } else if (data.info.type == "1") {//事故疑点
                $("#stnNameLi").hide();
                $("#drvSctBhvLi").hide();
                $("#occuTime1Li").hide();
                $("#occuTime2Li").show();

                $("#licPltNo").html(data.info.licPltNo);
                $("#drvName").html(data.info.drvName);
                $("#lineName").html(data.info.lineName);
                $("#occuTime2").html(data.info.occuTime);
            }
        }

        initDobInfo();

        // 设置x轴刻度
        var categories = [];

        var initCategories = function () {
            // 设置为空值
            categories = [];
            var scale = 0;
            for (var i = 0; i < 101; i++) {
                categories.push(scale.toFixed(1));
                scale += 0.2;
            }
            categories.push(scale.toFixed(1));
        };

        //initCategories();
        categories = data.categories;

        // 设置y轴刻度
        var sudu = [],
            zhuansu = [],
            qianqiya = [],
            houqiya = [],
            shuiwen = [],
            youya = [],
            zongdianya = [],
            zongdianliu = [],
            dianjizhuansu = [],
            soc = [];

        // 初始化y轴刻度
        var suduInit = function () {
            // 设置为空值
            sudu = [];
            var scale = 0;
            for (var i = 0; i < 20; i++) {
                sudu.push(scale);
                scale += 10;
            }
            sudu.push(scale);
        };

        var zhuansuInit = function () {
            // 设置为空值
            zhuansu = [];
            var scale = 0;
            for (var i = 0; i < 20; i++) {
                zhuansu.push(scale);
                scale += 400;
            }
            zhuansu.push(scale);
        };

        var qianqiyaInit = function () {
            // 设置为空值
            qianqiya = [];
            var scale = 0;
            for (var i = 0; i < 20; i++) {
                qianqiya.push(scale.toFixed(1));
                scale += 0.1;
            }
            qianqiya.push(scale.toFixed(1));
        };

        var houqiyaInit = function () {
            // 设置为空值
            houqiya = [];
            var scale = 0;
            for (var i = 0; i < 20; i++) {
                houqiya.push(scale.toFixed(1));
                scale += 0.1;
            }
            houqiya.push(scale.toFixed(1));
        };

        var shuiwenInit = function () {
            // 设置为空值
            shuiwen = [];
            var scale = 0;
            for (var i = 0; i < 20; i++) {
                shuiwen.push(scale);
                scale += 10;
            }
            shuiwen.push(scale);
        };

        var youyaInit = function () {
            // 设置为空值
            youya = [];
            var scale = 0;
            for (var i = 0; i < 20; i++) {
                youya.push(scale);
                scale += 40;
            }
            youya.push(scale);
        };

        var zongdianyaInit = function () {
            // 设置为空值
            zongdianya = [];
            var scale = 0;
            for (var i = 0; i < 20; i++) {
                zongdianya.push(scale);
                scale += 80;
            }
            zongdianya.push(scale);
        };

        var zongdianliuInit = function () {
            // 设置为空值
            zongdianliu = [];
            var scale = -600;
            for (var i = 0; i < 20; i++) {
                zongdianliu.push(scale);
                scale += 120;
            }
            zongdianliu.push(scale);
        };

        var dianjizhuansuInit = function () {
            // 设置为空值
            dianjizhuansu = [];
            var scale = 0;
            for (var i = 0; i < 20; i++) {
                dianjizhuansu.push(scale);
                scale += 400;
            }
            dianjizhuansu.push(scale);
        };

        var socInit = function () {
            // 设置为空值
            soc = [];
            var scale = 0;
            for (var i = 0; i < 20; i++) {
                soc.push(scale);
                scale += 10;
            }
            soc.push(scale);
        };

        suduInit();
        zhuansuInit();
        qianqiyaInit();
        houqiyaInit();
        shuiwenInit();
        youyaInit();
        zongdianyaInit();
        zongdianliuInit();
        dianjizhuansuInit();
        socInit();

        $('#container').highcharts({
            chart: {
                zoomType: 'xy' //
            },
            title: {
                text: ''
            },
            subtitle: {
                text: ''
            },
            //x轴的设置
            xAxis: [{
                labels: {
                    //step: 5,
                    formatter: function () {
                        return categories[this.value];
                    }
                },
                title: {
                    text: '持续时长（秒）'
                }
            }],
            //y轴的设置
            yAxis: [{ // 速度
                gridLineWidth: 0,
                title: {
                    text: '速度(km/h)',
                    style: {
                        color: '#2c82c9'
                    }
                },
                labels: {
                    style: {
                        color: '#2c82c9'
                    },
                    formatter: function () {
                        if (this.value == 110) {
                            return '雾灯开关';
                        }
                        if (this.value == 120) {
                            return '远光开关';
                        }
                        if (this.value == 130) {
                            return '近灯开关';
                        }
                        if (this.value == 140) {
                            return '右转向灯开关';
                        }
                        if (this.value == 150) {
                            return '左转向灯开关';
                        }
                        if (this.value == 160) {
                            return '脚刹开关';
                        }
                        if (this.value == 170) {
                            return '手刹开关';
                        }
                        if (this.value == 180) {
                            return 'ABS工作状态';
                        }
                        if (this.value == 190) {
                            return '喇叭开关';
                        }
                        if (this.value == 200) {
                            return 'ACC开关';
                        }
                        return this.value;
                    }
                },
                tickPositions: sudu,
                lineWidth: 1
            },
                { // 前气压
                    labels: {
                        formatter: function () {
                            if (this.value > 1) {
                                return '';
                            }
                            return this.value;
                        },
                        style: {
                            color: '#EEE657'
                        }
                    },
                    title: {
                        text: '前气压(mPa)',
                        style: {
                            color: '#EEE657'
                        }
                    },
                    tickPositions: qianqiya
                },
                { // 后气压
                    gridLineWidth: 0,
                    title: {
                        text: '后气压(mPa)',
                        style: {
                            color: '#FCB941'
                        }
                    },
                    labels: {
                        formatter: function () {
                            if (this.value > 1) {
                                return '';
                            }
                            return this.value;
                        },
                        style: {
                            color: '#FCB941'
                        }
                    },
                    tickPositions: houqiya
                },
                { // 油压
                    labels: {
                        formatter: function () {
                            if (this.value > 400) {
                                return '';
                            }
                            return this.value;
                        },
                        style: {
                            color: '#FC6042'
                        }
                    },
                    title: {
                        text: '油压(kPa)',
                        style: {
                            color: '#FC6042'
                        }
                    },
                    tickPositions: youya

                },
                { // 水温
                    labels: {
                        formatter: function () {
                            if (this.value > 100) {
                                return '';
                            }
                            return this.value;
                        },
                        style: {
                            color: '#3BC5ED'
                        }
                    },
                    title: {
                        text: '水温(°C)',
                        style: {
                            color: '#3BC5ED'
                        }
                    },
                    tickPositions: shuiwen
                },
                { // 转速
                    labels: {
                        formatter: function () {
                            if (this.value > 4000) {
                                return '';
                            }
                            return this.value;
                        },
                        style: {
                            color: '#D87737'
                        }
                    },
                    title: {
                        text: '转速(r/min)',
                        style: {
                            color: '#D87737'
                        }
                    },
                    tickPositions: zhuansu,
                    opposite: true
                },
                { // 总电流
                    labels: {
                        formatter: function () {
                            if (this.value > 600) {
                                return '';
                            }
                            return this.value;
                        },
                        style: {
                            color: '#53DC60'
                        }
                    },
                    title: {
                        text: '总电流(A)',
                        style: {
                            color: '#53DC60'
                        }
                    },
                    tickPositions: zongdianliu,
                    opposite: true
                },
                { // 总电压
                    labels: {
                        formatter: function () {
                            if (this.value > 800) {
                                return '';
                            }
                            return this.value;
                        },
                        style: {
                            color: '#8771CF'
                        }
                    },
                    title: {
                        text: '总电压(V)',
                        style: {
                            color: '#8771CF'
                        }
                    },
                    tickPositions: zongdianya,
                    opposite: true
                },
                { // 电机转速
                    labels: {
                        formatter: function () {
                            if (this.value > 4000) {
                                return '';
                            }
                            return this.value;
                        },
                        style: {
                            color: '#35B6CD'
                        }
                    },
                    title: {
                        text: '电机转速(r/min)',
                        style: {
                            color: '#35B6CD'
                        }
                    },
                    tickPositions: dianjizhuansu,
                    opposite: true
                },
                { // SOC
                    labels: {
                        formatter: function () {
                            if (this.value > 100) {
                                return '';
                            }
                            return this.value;
                        },
                        style: {
                            color: '#E25979'
                        }
                    },
                    title: {
                        text: 'SOC(%)',
                        style: {
                            color: '#E25979'
                        }
                    },
                    tickPositions: soc,
                    opposite: true
                }],
            // 数据线
            series: [{
                name: '速度',
                color: '#2c82c9',
                type: 'spline',
                yAxis: 0,
                data: data.sudu,
                marker: {
                    enabled: false
                },
                tooltip: {
                    valueSuffix: 'km/h'
                }
            },
                {
                    name: '前气压',
                    color: '#EEE657',
                    type: 'spline',
                    yAxis: 1,
                    visible: false,
                    data: data.qianqiya,
                    marker: {
                        enabled: false
                    },
                    tooltip: {
                        valueSuffix: 'mPa'
                    }
                },
                {
                    name: '后气压',
                    color: '#FCB941',
                    type: 'spline',
                    yAxis: 2,
                    visible: false,
                    data: data.houqiya,
                    marker: {
                        enabled: false
                    },
                    tooltip: {
                        valueSuffix: 'mPa'
                    }
                },
                {
                    name: '油压',
                    color: '#FC6042',
                    type: 'spline',
                    yAxis: 3,
                    visible: false,
                    data: data.youya,
                    marker: {
                        enabled: false
                    },
                    tooltip: {
                        valueSuffix: 'kPa'
                    }
                },
                {
                    name: '水温',
                    color: '#3BC5ED',
                    type: 'spline',
                    yAxis: 4,
                    visible: false,
                    data: data.shuiwen,
                    marker: {
                        enabled: false
                    },
                    tooltip: {
                        valueSuffix: '°C'
                    }
                },
                {
                    name: '转速',
                    color: '#D87737',
                    type: 'spline',
                    yAxis: 5,
                    data: data.zhuansu,
                    marker: {
                        enabled: false
                    },
                    tooltip: {
                        valueSuffix: 'r/min'
                    }
                },
                {
                    name: '总电流',
                    color: '#53DC60',
                    type: 'spline',
                    yAxis: 6,
                    visible: false,
                    data: data.zongdianliu,
                    marker: {
                        enabled: false
                    },
                    tooltip: {
                        valueSuffix: 'A'
                    }
                },
                {
                    name: '总电压',
                    color: '#8771CF',
                    type: 'spline',
                    yAxis: 7,
                    visible: false,
                    data: data.zongdianya,
                    marker: {
                        enabled: false
                    },
                    tooltip: {
                        valueSuffix: 'V'
                    }
                },
                {
                    name: '电机转速',
                    color: '#35B6CD',
                    type: 'spline',
                    yAxis: 8,
                    visible: false,
                    data: data.dianjizhuansu,
                    marker: {
                        enabled: false
                    },
                    tooltip: {
                        valueSuffix: 'r/min'
                    }
                },
                {
                    name: 'SOC',
                    color: '#E25979',
                    type: 'spline',
                    yAxis: 9,
                    visible: false,
                    data: data.soc,
                    marker: {
                        enabled: false
                    },
                    tooltip: {
                        valueSuffix: '%'
                    }
                },
                // 开关量
                {
                    name: '雾灯开关',
                    color: '#2CC990',
                    type: 'spline',
                    yAxis: 0,
                    marker: {
                        enabled: false
                    },
                    showInLegend: false,
                    data: data.d1
                },
                {
                    name: '远光开关',
                    color: '#2CC990',
                    type: 'spline',
                    yAxis: 0,
                    marker: {
                        enabled: false
                    },
                    showInLegend: false,
                    data: data.d2
                },
                {
                    name: '近灯开关',
                    color: '#2CC990',
                    type: 'spline',
                    yAxis: 0,
                    marker: {
                        enabled: false
                    },
                    showInLegend: false,
                    data: data.d3
                },
                {
                    name: '右转向灯开关',
                    color: '#2CC990',
                    type: 'spline',
                    yAxis: 0,
                    marker: {
                        enabled: false
                    },
                    showInLegend: false,
                    data: data.d4
                },
                {
                    name: '左转向灯开关',
                    color: '#2CC990',
                    type: 'spline',
                    yAxis: 0,
                    marker: {
                        enabled: false
                    },
                    showInLegend: false,
                    data: data.d5
                },
                {
                    name: '脚刹开关',
                    color: '#2CC990',
                    type: 'spline',
                    yAxis: 0,
                    marker: {
                        enabled: false
                    },
                    showInLegend: false,
                    data: data.d6
                },
                {
                    name: '手刹开关',
                    color: '#2CC990',
                    type: 'spline',
                    yAxis: 0,
                    marker: {
                        enabled: false
                    },
                    showInLegend: false,
                    data: data.d7
                },
                {
                    name: 'ABS工作状态',
                    color: '#2CC990',
                    type: 'spline',
                    yAxis: 0,
                    marker: {
                        enabled: false
                    },
                    showInLegend: false,
                    data: data.d8
                },
                {
                    name: '喇叭开关',
                    color: '#2CC990',
                    type: 'spline',
                    yAxis: 0,
                    marker: {
                        enabled: false
                    },
                    showInLegend: false,
                    data: data.d9
                },
                {
                    name: 'ACC开关',
                    color: '#2CC990',
                    type: 'spline',
                    yAxis: 0,
                    marker: {
                        enabled: false
                    },
                    showInLegend: false,
                    data: data.d10
                }],
            // 提示信息
            tooltip: {
                shared: true,
                crosshairs: true,
                formatter: function () {
                    var content = '<b>第 ' + categories[this.x] + ' 秒<b/>';
                    var y = '';
                    $.each(this.points,
                        function (n, point) {
                            if (point.series.name == 'ACC开关' || point.series.name == '喇叭开关' || point.series.name == 'ABS工作状态' || point.series.name == '手刹开关' || point.series.name == '脚刹开关' || point.series.name == '左转向灯开关' || point.series.name == '右转向灯开关' || point.series.name == '近灯开关' || point.series.name == '远光开关' || point.series.name == '雾灯开关') {
                                y = "开";
                            } else if (point.series.name == '前气压' || point.series.name == '后气压') {
                                y = point.y.toFixed(2);
                            } else {
                                y = point.y;
                            }

                            content += '<br/><span style="color:' + point.series.color + ';">' + point.series.name + '<span/>: ' + y;
                        });

                    return content;
                }
            },
            //
            legend: {
                width: 750//,
                //enabled: false
            }
        });
        
        var mychart = $('#container').highcharts();
        
        var begintag = data.info.begintag == 0?1:data.info.begintag;
        var endtag = data.info.endtag == 0?1:data.info.endtag;
        var starttag = data.info.starttag == 0?1:data.info.starttag;

        //动态添加标线
        if (begintag) {
            mychart.xAxis[0].addPlotLine({
                label: {
                    text: '开始点',
                    align: 'left',
                    x: 10
                },
                value: begintag,
                width: 1,
                color: 'red',
                id: 'plot-line-1'
            });
        }

        if (endtag) {
            mychart.xAxis[0].addPlotLine({
                label: {
                    text: '结束点',
                    align: 'left',
                    x: 10
                },
                value: endtag,
                width: 1,
                color: 'red',
                id: 'plot-line-2'
            });
        }

        if (starttag) {
            mychart.xAxis[0].addPlotLine({
                label: {
                    text: '发生点',
                    align: 'left',
                    x: 10
                },
                value: starttag,
                width: 1,
                color: 'red',
                id: 'plot-line-3'
            });
        }
        
    };
});